import { useEffect, useRef, useState } from "react"

export const CountDown=(num=60)=>{
    const [count,setCount]=useState(num)
    const [isDown,setIsDown]=useState(false)
    const timerRef=useRef(null) 
    function start(){
        setIsDown(true)
        timerRef.current=setInterval(()=>{
            setCount(prev=>prev-1)
            console.log(count);
            
        },1000)
    }
    useEffect(()=>{
        if(count<=0){
            clearInterval(timerRef.current)
            setIsDown(true)
            
        } 
    },[count])

    useEffect(()=>{
        clearInterval(timerRef.current)
        setIsDown(false)
    },[])
    return {start,count,isDown}
}